﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Traceability.APP.Applications.Commands.Suyuan;
using Traceability.APP.Applications.Commands.YangZhi;
using Traceability.APP.Dtos;
using Traceability.Domain;
using Traceability.ErrorCode;

namespace Traceability.APP.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ManagementController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造函数(master)
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="mediator"></param>
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="mediator"></param>
        public ManagementController(ILogger<ManagementController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 获取溯源基本信息
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<SuyuanQuerydto>>> GetSuyuan(int index,int size)
        {
            try
            {
                logger.LogInformation("获取溯源基本信息成功");
                SuyuanQueryCommand command = new SuyuanQueryCommand();
                command.Index = index;
                command.Size = size;
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取溯源基本信息失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 溯源新增
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> AddSuyuan(SuyuanCreateCommand command)
        {
             return await mediator.Send(command);
        }
        /// <summary>
        /// 获取商品信息用于下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<ShopModel>>> GetShop()
        {
            ShopQueryCommand command=new ShopQueryCommand();
            return await mediator.Send(command);
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<string>> UploadFile(IFormFile file)
        {
            var res = new APIResult<string>();
            string saveDir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img");
            if (!Directory.Exists(saveDir))
                Directory.CreateDirectory(saveDir);

            string savefile = Path.Combine(saveDir, file.FileName);
            using (FileStream fs = new FileStream(savefile, FileMode.Create))
            {
                await file.CopyToAsync(fs);
                await fs.FlushAsync();
            }
            // 这里一定要有 /img/ 和斜杠
            string url = $"http://localhost:5012/img/{file.FileName}";
            return new APIResult<string>()
            {
                Code = APIEnum.AddSuccess,
                Message = "上传成功",
                Data = url
            };
        }
        /// <summary>
        /// 溯源修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> UpdSuyuan(SuyuanUpdCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 溯源删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> DelSuyuan(SuyuanDelCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DelAll(SuyuanPiDelCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 溯源详情
        /// </summary>
        /// <param name="Batch"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<SuyuanQuerydto>> GetXiangqing(string Batch)
        {
            SuyuanXiangqingCommand command=new SuyuanXiangqingCommand();
            command.Batch = Batch;
            return await mediator.Send(command);
        }
        /// <summary>
        /// 养殖详情
        /// </summary>
        /// <param name="Batch"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<FarmModel>> GetFarmXiangqing(string Batch)
        {
            YangZhiXIangqingCommand command = new YangZhiXIangqingCommand();
            command.Batch = Batch;
            return await mediator.Send(command);
        }
        /// <summary>
        /// 养殖新增
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddFarm(YangZhiAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 养殖修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdFarm(YangZhiUpdCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 保健信息详情
        /// </summary>
        /// <param name="Batch"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<HealthModel>>> GetHealthXiangqing(string Batch)
        {
            HealthQueryCommand command = new HealthQueryCommand();
            command.Batch = Batch;
            return await mediator.Send(command);
        }
        /// <summary>
        /// 保健信息新增
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddHealth (HealthAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 保健信息修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdHealth(HealthUpdCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 养殖企业信息详情
        /// </summary>
        /// <param name="Batch"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<FarmCompanyModel>> GetFarmCompanyXiangqing(string Batch)
        {
            FarmCompanyQueryCommand command = new FarmCompanyQueryCommand();
            command.Batch = Batch;
            return await mediator.Send(command);
        }
        /// <summary>
        /// 保健信息删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DelHealth(HealthDelCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 养殖企业信息新增
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddFarmCompany(FarmCompanyAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 养殖企业信息修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdFarmCompany(FarmCompanyUpdCommand command)
        {
            return await mediator.Send(command);
        }
    }
}